<?php
class Trip extends CI_Model {
	function addTripInfo($name, $id_User, $dateStart) {
		$data = array ('id_User' => $id_User, 'name' => $name, 'dateStart' => $dateStart );
		$this->db->insert ( 'trip', $data );
		return $this->db->insert_id ();
	}
	function updateTrip($id_Trip, $duration) {
		$data = array ('duration' => $duration );
		$this->db->where ( 'id_Trip', $id_Trip );
		$this->db->update ( 'trip', $data );
	}
	function updateTripTime($id_Trip, $startDate) {
		$data = array ('dateStart' => $startDate );
		$this->db->where ( 'id_Trip', $id_Trip );
		$this->db->update ( 'trip', $data );
	}
	function addTripDetailHotel($id_Trip, $day, $id_Hotel, $des) {
		$data = array ('id_Trip' => $id_Trip, 'day' => $day, 'id_Hotel' => $id_Hotel, 'description' => $des );
		$this->db->insert ( 'tripdetail', $data );
		return $this->db->insert_id ();
	}
	function addTripDetailResEvent($id_TripDetail, $id_Obj, $time, $id_Type) {
		if ($id_Type == 2) {
			$data = array ('id_TripDetail' => $id_TripDetail, 'id_Res' => $id_Obj, 'time' => $time );
			$this->db->insert ( 'tripdetailres', $data );
		} else if ($id_Type == 3) {
			$data2 = array ('id_TripDetail' => $id_TripDetail, 'id_Event' => $id_Obj, 'time' => $time );
			$this->db->insert ( 'tripdetailevent', $data2 );
		}
		
		return $this->db->insert_id ();
	}
	function getTrip($id_User) {
		$this->db->select ( '*' );
		$this->db->from ( 'trip' );
		$this->db->where ( 'id_User', $id_User );
		$this->db->order_by ( 'dateStart desc' );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1)
			return $query->result ();
		else
			return FALSE;
	}
	function getTripDetailHotel($id_Trip, $day) {
		$data = array ('id_Trip' => $id_Trip, 'day' => $day );
		$this->db->select ( '*' );
		$this->db->from ( 'tripdetail' );
		$this->db->where ( $data );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1)
			return $query->result ();
		else
			return FALSE;
	}
	function getTripDetailRes($id_TripDetail) {
		$this->db->select ( '*' );
		$this->db->from ( 'tripdetailres' );
		$this->db->where ( 'id_TripDetail', $id_TripDetail );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1)
			return $query->result ();
		else
			return FALSE;
	}
	function getTripDetailEvent($id_TripDetail) {
		$this->db->select ( '*' );
		$this->db->from ( 'tripdetailevent' );
		$this->db->where ( 'id_TripDetail', $id_TripDetail );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1)
			return $query->result ();
		else
			return FALSE;
	}
	function getTripByHotel($id_Hotel) {
		$this->db->select ( '*' );
		$this->db->from ( 'tripdetail' );
		$this->db->where ( 'id_Hotel', $id_Hotel );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1)
			return $query->result ();
		else
			return FALSE;
	}
	function getTripById($id_Trip) {
		$this->db->select ( '*' );
		$this->db->from ( 'trip' );
		$this->db->where ( 'id_Trip', $id_Trip );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1)
			return $query->result ();
		else
			return FALSE;
	}
	function updateTripDetailHotel($id_TripDetail, $id_Hotel, $description) {
		$data = array ('id_Hotel' => $id_Hotel, 'description' => $description );
		$this->db->where ( 'id_TripDetail', $id_TripDetail );
		$this->db->update ( 'tripdetail', $data );
	
	}
	function updateTripDetailResEvent($id_TripDetail, $id_Obj, $time, $id_Type) {
		if ($id_Type == 2) {
			
			$data = array ('id_TripDetail' => $id_TripDetail, 'id_Res' => $id_Obj, 'time' => $time );
			$this->db->insert ( 'tripdetailres', $data );
		
		} else if ($id_Type == 3) {
			
			$data = array ('id_TripDetail' => $id_TripDetail, 'id_Event' => $id_Obj, 'time' => $time );
			$this->db->insert ( 'tripdetailevent', $data );
		
		}
	
	}
	function deleteTripDetail($id_TripDetail, $id_Type) {
		if ($id_Type == 2) {
			$this->db->where ( 'id_TripDetail', $id_TripDetail );
			$this->db->delete ( 'tripdetailres' );
		} else if ($id_Type == 3) {
			$this->db->where ( 'id_TripDetail', $id_TripDetail );
			$this->db->delete ( 'tripdetailevent' );
		}
	}
	function getTripDetailByResEvent($id_Obj, $type) {
		if ($type == 2) {
			$this->db->select ( '*' );
			$this->db->from ( 'tripdetailres' );
			$this->db->where ( 'id_Res', $id_Obj );
		} else if ($type == 3) {
			$this->db->select ( '*' );
			$this->db->from ( 'tripdetailevent' );
			$this->db->where ( 'id_Event', $id_Obj );
		}
		$query = $this->db->get ();
		if ($query->num_rows () >= 1)
			return $query->result ();
		else
			return FALSE;
	
	}
	function getTripByIdTripDetail($id_TripDetail) {
		$this->db->select ( '*' );
		$this->db->from ( 'tripdetail' );
		$this->db->where ( 'id_TripDetail', $id_TripDetail );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1)
			return $query->result ();
		else
			return FALSE;
	}
}